"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const requestPromise = require("request-promise-native");
let request = requestPromise.defaults({ json: true });
const config = require("./../../config.js");
const utility_1 = require("./../utility");
let sectionMap = {}; //key-value 区间名称-区间的id
const Seperator = "～";
/** 从顺凯那边把项目拉过来 */
function fetchLineFromRemoteAsync(lineName) {
    return request({ url: `${config.skyUrl}/GetDeclareItemListByList?MetroLineName=${encodeURIComponent(lineName)}` })
        .then(res => {
        while (typeof res === 'string')
            res = JSON.parse(res);
        return res;
    }, err => null).then(res => {
        if (res && res.lines && res.lines.length) {
            return utility_1.toCamel(res.lines[0]);
        }
        else {
            return { "city": "上海", "lineId": 1, "name": lineName, "sections": [] };
        }
    }).then(line => {
        line.sections.forEach(section => {
            //将string的url替换成array
            section.projects.forEach(prj => {
                if (!prj.filePath || !prj.filePath.length)
                    return;
                let imageList = prj.filePath.split(',');
                imageList.forEach((url, index) => {
                    if (!index)
                        return;
                    imageList[index] = 'http://120.25.160.53/skxs_files/' + url;
                });
                delete prj.filePath;
                prj.imageList = imageList;
            });
            /** @todo */
            //顺凯用的是百度坐标系，需要转换成火星坐标系
        });
        return line;
    });
}
exports.fetchLineFromRemoteAsync = fetchLineFromRemoteAsync;
/** 从缓存，或者从远程拉取section */
function getSectionAsync(sectionName) {
    return __awaiter(this, void 0, void 0, function* () {
        let cache = sectionMap[sectionName];
        if (cache)
            return cache;
        let stations = sectionName.split(Seperator);
        if (stations.length !== 2)
            return null;
        let url = `${config.apiUrl}/section/query`;
        let section = yield request({ url, qs: { station1: stations[0], station2: stations[1] } }).then(res => res, err => null);
        if (!section)
            return null;
        return section;
    });
}
exports.getSectionAsync = getSectionAsync;
function uploadToMetroProjects(recordItem) {
    return __awaiter(this, void 0, void 0, function* () {
        let url = `${config.apiUrl}/Inspect/Record`;
        let ret = yield request({
            method: 'post',
            url,
            json: recordItem
        }).then(res => res, err => err);
    });
}
exports.uploadToMetroProjects = uploadToMetroProjects;
